{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "/home/leon/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input, SeparableConv2D, Activation, BatchNormalization\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_seed = 10014\n",
    "data_in_shape = (7, 7, 1)\n",
    "\n",
    "layers = [\n",
    "    SeparableConv2D(2, (3,3), padding='valid', strides=(1,1)),\n",
    "    SeparableConv2D(2, (3,3), padding='valid', strides=(1,1))\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_14'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/14.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_14\": {\"input\": {\"data\": [0.488004, 0.911709, 0.385817, 0.735948, 0.007869, -0.35278, 0.875938, -0.016655, 0.5483, -0.074508, 0.164805, 0.364344, 0.113199, -0.963444, 0.249173, 0.518158, -0.131317, 0.700476, 0.406088, -0.846826, 0.29394, 0.082756, -0.416403, 0.499608, -0.800435, 0.920522, 0.310248, -0.303414, -0.908233, 0.203626, -0.241686, -0.086586, -0.594866, -0.396726, -0.326546, -0.303103, 0.464722, -0.565768, -0.125654, 0.077641, 0.633304, 0.211824, -0.108683, -0.613915, 0.907336, -0.708153, 0.447691, 0.77094, 0.796293], \"shape\": [7, 7, 1]}, \"weights\": [{\"data\": [0.488004, 0.911709, 0.385817, 0.735948, 0.007869, -0.35278, 0.875938, -0.016655, 0.5483], \"shape\": [3, 3, 1, 1]}, {\"data\": [0.263717, -0.720003], \"shape\": [1, 1, 1, 2]}, {\"data\": [-0.296519, 0.572188], \"shape\": [2]}, {\"data\": [-0.983721, 0.040472, -0.976147, -0.023768, -0.481133, -0.192315, 0.766864, -0.700711, -0.549223, 0.449282, 0.713811, 0.290114, -0.96995, 0.09209, -0.529109, -0.916857, 0.914016, -0.884145], \"shape\": [3, 3, 2, 1]}, {\"data\": [0.524698, 0.474407, 0.471901, 0.222861], \"shape\": [1, 1, 2, 2]}, {\"data\": [-0.040091, -0.549528], \"shape\": [2]}], \"expected\": {\"data\": [0.055576, -0.469849, -1.299474, -1.284736, -0.554166, -0.719936, -0.517287, -0.698947, 0.015128, -0.407083, 0.259625, -0.344306, -0.861422, -0.872735, -0.40709, -0.565243, -0.880509, -0.777895], \"shape\": [3, 3, 2]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
